package fun.flyee.android.pengshan.acts;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.hcvsa.MyHCVSA;
import com.hcvsa.OnCMSLoginStatus;
import com.hcvsa.UserAcceptCall;
import com.hcvsa.UserHandUpCall;
import com.hcvsa.UserOffline;
import com.hcvsa.UserRejectCall;
import com.hcvsa.UserBusyCall;
import com.squareup.otto.Subscribe;
import com.until.BusProvider;
import com.view.MyVideoRel2;

import butterknife.BindView;
import butterknife.OnClick;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.pengshan.social.R;
import hcvs.hcvsa.MeetingEvent;

public class DialogActivity extends BaseNotifyActivity {

    @BindView(R.id.tip_view)
    public TextView tipView;

    private int id;        //用户回话ID，每个在线用户拥有唯一的sessionid
    private int type;
    private String tip;
    private int mode; // 1 call 2 conference

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            try {
                reject();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    protected int layoutId() {
        return R.layout.act_dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕不息屏
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);//点亮屏幕
        Systems.hideSystemNavBar(this);
        if (Build.VERSION.SDK_INT > 27) {
            setShowWhenLocked(true);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        }
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        getWindow().setAttributes(params);
        BusProvider.getInstance().register(this);
        id = getIntent().getIntExtra("id", 0);
        type = getIntent().getIntExtra("type", 0);
        tip = getIntent().getStringExtra("tip");
        mode = getIntent().getIntExtra("mode", 0);
        ring = Systems.playRing(this);
        tipView.setText(tip);
        handler.sendEmptyMessageDelayed(0, 30000);
    }

    @Override
    public void onStop() {
        super.onStop();
        releaseRing();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        try {
            handler.removeMessages(0);
            handler = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            BusProvider.getInstance().unregister(this);
        } catch (Exception e) {
            Logger.e("unregister failed", e);
        }
        super.onDestroy();
    }

    @Subscribe
    public void UserBusyCall(UserBusyCall data) {
        Toast.makeText(this, "用户忙线中", Toast.LENGTH_LONG).show();
        finish();
    }

    @Subscribe
    public void UserRejectCall(UserRejectCall data) {
        Toast.makeText(this, "用户拒绝了你的呼叫", Toast.LENGTH_LONG).show();
        finish();
    }

    @Subscribe
    public void UserHangup(UserHandUpCall data) {
        Toast.makeText(this, "用户挂断了你的呼叫", Toast.LENGTH_LONG).show();
        finish();
    }

    @Subscribe
    public void UserOffline(UserOffline data) {
        if (id == data.getUser().getSessionid()) {
            Toast.makeText(this, "用户下线了！", Toast.LENGTH_LONG).show();
            finish();
        }
    }

    @Subscribe
    public void onCMSLoginStatus(OnCMSLoginStatus data) {
        if (data.getLoginStatus() == 0) {
            Toast.makeText(this, "与服务器断开连接！", Toast.LENGTH_LONG).show();
            try {
                MyHCVSA.getInstance().getUserSessionMoudle().OnMyRejectCall(id, type);
            } catch (Exception e) {
                e.printStackTrace();
            }
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //挂断
            this.reject();
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }

    @OnClick(R.id.reject_btn)
    public void reject() {
        releaseRing();
        MyHCVSA.getInstance().getUserSessionMoudle().OnMyRejectCall(id, type);
        Router.main(this);
        finish();
    }

    @OnClick(R.id.accept_btn)
    public void accept() {
        releaseRing();
        if (mode == 1) {
            Router.acceptCall(this, id, type, tip);
        } else if (mode == 2) {
            Router.joinConference(this, id, tip);
        }
        finish();
    }

}
